﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WorldBankBBS.Data
{
    public class ModelHelper : IDisposable
    {
        private WorldBankBBSEntities _context = null;

        public WorldBankBBSEntities Context
        {
            get { return _context; }
            private set { _context = value; }
        }

        public WorldBankBBSEntities BeginNewContext()
        {
            Context = new WorldBankBBSEntities();

            return Context;
        }

        public void EndCurrentContext()
        {
            if (Context != null)
            {
                Context.Dispose();
            }
        }

        public Log AddToLogs(Log log)
        {
            if(log == null) throw new ArgumentNullException("log");
            if(Context == null) throw new ApplicationException("Must initialize context first.");

            Context.Logs.Add(log);

            return log;
        }

        public Session AddToSessions(Session session)
        {
            if (session == null) throw new ArgumentNullException("session");
            if (Context == null) throw new ApplicationException("Must initialize context first.");

            Context.Sessions.Add(session);

            return session;
        }

        public void Dispose()
        {
            EndCurrentContext();
        }
    }
}
